package com.blog.service.site.impl;

import com.blog.constant.Types;
import com.blog.dto.StatisticsDto;
import com.blog.dto.cond.CommentCondition;
import com.blog.dto.cond.ContentCondition;
import com.blog.mapper.AttachMapper;
import com.blog.mapper.CommentMapper;
import com.blog.mapper.ContentMapper;
import com.blog.mapper.MetaMapper;
import com.blog.model.Comment;
import com.blog.model.Content;
import com.blog.service.site.SiteService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 站点服务的业务接口实现类
 *
 * @author wmb
 *
 * @since 2021-2-8
 */
@Service("siteService")
@Transactional
public class SiteServiceImpl implements SiteService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SiteServiceImpl.class);

    @Autowired
    @Qualifier("commentMapper")
    private CommentMapper commentMapper;

    @Autowired
    @Qualifier("contentMapper")
    private ContentMapper contentMapper;

    @Autowired
    @Qualifier("metaMapper")
    private MetaMapper metaMapper;

    @Autowired
    @Qualifier("attachMapper")
    private AttachMapper attachMapper;

    /**
     * 获取评论列表
     *
     * @param limit
     *
     * @return
     */
    @Override
    @Cacheable(value = "siteCache", key = "'comments_' + #p0" )
    public List<Comment> getComments(Integer limit) {
        LOGGER.debug("Enter recentComments method:limit={}", limit);
        if (limit < 0 || limit > 10) {
            limit = 10;
        }
        PageHelper.startPage(1, limit);
        // 获取status为approved的评论
        List<Comment> commentList = commentMapper.getCommentsByCondition(new CommentCondition("approved"));
        LOGGER.debug("Exit recentComments method");
        return commentList;
    }

    /**
     * 获取最新的文章
     *
     * @param limit
     *
     * @return
     */
    @Override
    @Cacheable(value = "siteCache", key = "'newArticles_' + #p0")
    public List<Content> getNewArticles(Integer limit) {
        LOGGER.debug("Enter recentArticles method:limit={}", limit);
        if (limit < 0 || limit > 10) {
            limit = 10;
        }
        PageHelper.startPage(1, limit);
        // 获取status为publish的文章
        List<Content> contentList = contentMapper.getArticlesByCondition(new ContentCondition("publish"));
        LOGGER.debug("Exit recentArticles method");
        return contentList;
    }

    /**
     * 获取 后台统计数据
     *
     * @return
     */
    @Override
    @Cacheable(value = "siteCache", key = "'statistics_'")
    public StatisticsDto getStatistics() {
        LOGGER.debug("Enter recentStatistics method");

        // 文章总数
        Long articleNum = contentMapper.getArticleCount();
        // 评论总数
        Long commentNum = commentMapper.getCommentsCount();
        // 链接数
        Long linkNum = metaMapper.getMetasCountByType(Types.LINK.getType());
        //
        Long attachmentNum = attachMapper.getAttachesCount();

        StatisticsDto statisticsDto = new StatisticsDto();
        statisticsDto.setArticles(articleNum);
        statisticsDto.setAttachments(attachmentNum);
        statisticsDto.setComments(commentNum);
        statisticsDto.setLinks(linkNum);

        LOGGER.debug("Exit recentStatistics method");
        return statisticsDto;
    }
}
